/*
 * Gestures.cpp
 *
 *  Created on: Jan 22, 2011
 *      Author: kmueller
 */

#include "Gestures.h"

#include <QWidget>
#include <QMouseEvent>
#include <QtCore>

CircleGesture::CircleGesture(QWidget *parent) :
	QWidget(parent) {
	enabled = false;
	recognized = false;
}

CircleGesture::~CircleGesture() {

}

CircleGesture::Direction CircleGesture::findDirection(QMouseEvent *event) {
	QPoint current = event->pos();

	int diffX = last.x() - current.x();
	int diffY = last.y() - current.y();

	if (abs(diffX) > abs(diffY)) {
		if (last.x() > current.x()) {
			return Left;
		}
		return Right;
	} else {
		if (last.y() > current.y()) {
			return Up;
		}
		return Down;
	}
}

CircleGesture::CircleDirection CircleGesture::findCircle(QMouseEvent *event) {
	return Clockwise;
}

void CircleGesture::recognize(QMouseEvent *event) {
	if (enabled == false)
		return;

	// TODO: implement
	recognized = true;
}

void CircleGesture::handle(QMouseEvent *event) {

	if (event->type() == QEvent::MouseButtonPress && event->button()
			== Qt::LeftButton) {
		enabled = true;
		recognized = false;
		last = event->pos();
		return;
	}
	if (event->type() == QEvent::MouseButtonRelease) {
		enabled = false;
		recognized = false;
		return;
	}
	if (event->type() == QEvent::MouseMove && enabled) {
		if (!recognized) {
			recognize(event);
		} else {

			// TODO: recognize circle moving direction and emit turnRight() as well
			//			emit turnLeft();

			//			event->accept();
		}
		last = event->pos();
	}
}

MoveGesture::MoveGesture(QWidget *parent) :
	QWidget(parent) {
}

MoveGesture::~MoveGesture() {

}

void MoveGesture::handle(QMouseEvent* event) {

	if (event->type() == QEvent::MouseButtonPress && event->button()
			== Qt::LeftButton) {
		enabled = true;
		//			recognized = false;
		start = event->pos();
		last = event->pos();
		return;
	}
	if (event->type() == QEvent::MouseButtonRelease) {
		enabled = false;
		//			recognized = false;
		QPoint end = event->pos();

		emit stopped(end - start);
		return;
	}
	if (event->type() == QEvent::MouseMove && enabled) {
		QPoint tmp;
		tmp.setX(event->pos().x() - last.x());
		tmp.setY(event->pos().y() - last.y());

		emit moved(tmp);

		last = event->pos();
	}
}
